package kotlinx.coroutines.script.analysis

import android.graphics.Bitmap
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.CComponents

object AnalysisInvite {
    private var recording = false
    private var lastRecordTs = 0L

    fun invoke(ss: Bitmap?): Boolean {
        if (ss == null) return false
        return Analysis.hit(ss, CComponents.checkInviteCancel, HitColors.hitRedButtonLeftPart1, 2, 0.5f) &&
               Analysis.hit(ss, CComponents.checkInviteCancelTop, HitColors.hitInviteCancelTop, 2, 0.4f) &&
               Analysis.hit(ss, CComponents.checkInviteConfirm, HitColors.hitRedButtonLeftPart1, 2, 0.5f)
    }

    fun conversion(ss: Bitmap?, rectBlock: Block): Boolean {
//        if (ss == null) return false
//        val c = Properties.inviteAutoAccept // 0拒绝. x同意需要的参数
//        val allRefuse = (c == 0)
//        if (allRefuse) return false
//        val allAccept = (c == 3)
//        if (allAccept) return true
//
//        val bmpOrigin = clipBitmap(ss, rectBlock.rect) ?: return false
//        val bmp = ImgPretreatment.doPretreatment(bmpOrigin)
//
//        val data = StringBuilder()
//        val samplingPx = 5.pt
//        var x = 0
//        while(x < bmp.width) {
//            var y = 0
//            while(y < bmp.height) {
//                data.append(App.take().takePixel(ss, x, y).toString())
//                y += samplingPx
//            }
//            x += samplingPx
//        }
//        val dataId = data.toString().md5
//
//        if (c == 1) { // 收录名单并同意 前20分钟
//            if (System.currentTimeMillis() - xScriptStartTs < 1200000) {
//                recording = true
//
//                if (Friends.dataList.size > 100) {
//                    ConsoleLayout.outputText("名单已满", Color.YELLOW)
//                } else {
//                    if (System.currentTimeMillis() - lastRecordTs > 5000) {
//                        ConsoleLayout.outputText("名单收录1", Color.GREEN)
//                        ConsoleLayout.outputText("名单收录2", Color.GREEN)
//                        ConsoleLayout.outputText("名单收录3", Color.GREEN)
//                        appScopeLaunch(Dispatchers.IO) {
//                            Friends.import(dataId, bmp)
//                        }
//                    }
//                }
//                lastRecordTs = System.currentTimeMillis()
//                return true // 10分钟内自动全同意
//            } else {
//                if (recording) {
//                    ConsoleLayout.outputText("邀请名单收录结束1", Color.YELLOW)
//                    ConsoleLayout.outputText("邀请名单收录结束2", Color.YELLOW)
//                    ConsoleLayout.outputText("邀请名单收录结束3", Color.YELLOW)
//                }
//                recording = false
//            }
//        }
//
//        // 对比名单
//        val block = Block("", 0, 0, bmp.width, bmp.height)
//        val nameImageList = Friends.dataList.map { it.filePath }
//        nameImageList.forEach {
//            val diskBmp = BitmapFactory.decodeFile(it)
//            if (diskBmp.width != bmp.width || diskBmp.height != bmp.height) {
//                // continue. 图片模式不一样
//            } else {
//                if (Analysis.compareWithLast(bmp, diskBmp, block, 3.pt, 0.85f, 3.pt)) {
//                    printLog { "AnalysisInvite conversion [mode:2] find same" }
//                    return true
//                }
//            }
//        }
//        return false
        return false
    }
}